import { _decorator, Component, Node, Sprite, resources, SpriteFrame, UITransform, Layers } from 'cc'
const { ccclass, property } = _decorator

import { TILE_TYPE_ENUM } from '../../Enums'

// 瓦片的宽高
export const TILE_WIDTH = 55
export const TILE_HEIGHT = 55

// spriteFrame是图片精灵图
// i是index
// j是index

// 每一个瓦片
@ccclass('TileManager')
export class TileManager extends Component {
  type: TILE_TYPE_ENUM | null = null

  moveable: boolean = false // 是否可以移动
  turnable: boolean = false // 是否可以转向

  async init({ type, spriteFrame, i, j }) {
    this.setType(type)

    // 创建图片节点
    const sprite = this.node.addComponent(Sprite)
    sprite.spriteFrame = spriteFrame

    // 设置宽高
    this.getComponent(UITransform).setContentSize(TILE_WIDTH, TILE_HEIGHT)

    // cocos是以左下角为原点
    const x = i * TILE_WIDTH
    const y = -j * TILE_HEIGHT
    this.node.setPosition(x, y, 0)
  }

  // 设置瓦片类型
  setType(type: TILE_TYPE_ENUM) {
    this.type = type

    if ([TILE_TYPE_ENUM.WALL_ROW, TILE_TYPE_ENUM.WALL_COLUMN, TILE_TYPE_ENUM.WALL_LEFT_TOP, TILE_TYPE_ENUM.WALL_LEFT_BOTTOM, TILE_TYPE_ENUM.WALL_RIGHT_TOP, TILE_TYPE_ENUM.WALL_RIGHT_BOTTOM].indexOf(type) > -1) {
      this.moveable = false
      this.turnable = false
    } else if ([TILE_TYPE_ENUM.CLIFF_CENTER, TILE_TYPE_ENUM.CLIFF_LEFT, TILE_TYPE_ENUM.CLIFF_RIGHT].indexOf(type) > -1) {
      this.moveable = false
      this.turnable = true
    } else if (type === TILE_TYPE_ENUM.FLOOR) {
      this.moveable = true
      this.turnable = true
    }
  }
}
